package com.unity3d.drawer;

import androidx.appcompat.app.AppCompatActivity;

import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;

import com.unity3d.drawer.databinding.ActivityMainBinding;

import java.io.IOException;

public class MainActivity extends AppCompatActivity {

    //viewBinding
    //自动将xml文件生成java类  类的名字以xml的名字+Binding
    ActivityMainBinding activityMainBinding;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //改造activity
        activityMainBinding= ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(activityMainBinding.getRoot());


        MediaPlayer mediaPlayer=new MediaPlayer();
        try {
            //设置音乐资源路径
            mediaPlayer.setDataSource(getAssets().openFd("a1.mp3"));
            //加载音乐资源
            mediaPlayer.prepare();
        } catch (IOException e) {
            e.printStackTrace();
        }

        activityMainBinding.button.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View view) {
                mediaPlayer.start();
                activityMainBinding.drawerLayout.openDrawer(Gravity.LEFT);
            }
        });

        activityMainBinding.button2.setOnClickListener(
                view->{
                    activityMainBinding.drawerLayout.closeDrawers();
                    mediaPlayer.pause();
                }
        );

    }
}